package com.ymm.lulutong.domain.impl;

import com.ymm.lulutong.constant.ProfileCodeEnum;
import com.ymm.lulutong.constant.ResultCodeEnum;
import com.ymm.lulutong.domain.ShipperCargoDomainService;
import com.ymm.lulutong.domain.ShipperWalletDomainService;
import com.ymm.lulutong.domain.utils.AddressUtils;
import com.ymm.lulutong.domain.utils.FormatUtils;
import com.ymm.lulutong.entity.Shipper;
import com.ymm.lulutong.entity.WalletOpsLog;
import com.ymm.lulutong.mapper.ShipperProfileMapper;
import com.ymm.lulutong.repo.SystemDefinePriceServiceRepo;
import com.ymm.lulutong.repo.SystemShipperCargoServiceRepo;
import com.ymm.lulutong.request.CargoDefinePriceWebRequest;
import com.ymm.lulutong.utils.Result;
import com.ymm.system.api.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.FileNotFoundException;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * @author rg
 */
@Slf4j
@Service
public class ShipperCargoDomainServiceImpl implements ShipperCargoDomainService {
    @Resource
    private SystemShipperCargoServiceRepo systemShipperCargoServiceRepo;

    @Resource
    private ShipperProfileMapper shipperProfileMapper;

    private static final int CARGO_DESCRIPTION_MAX_LENGTH = 50;
    private static final Integer MAX_PERSON_NUM = 3;
    private static final int CARGO_COMMENT_MAX_LENGTH = 50;


    /**
     * 发货
     *
     * @param putOnCargoRequest 发货Request
     * @param shipperId         货主Id
     * @return 货源Id
     * @throws Exception 异常
     */
    @Override
    public Result<Long> putOnCargo(PutOnCargoRequest putOnCargoRequest, Long shipperId) throws Exception {
        //参数校验
        if (Objects.isNull(putOnCargoRequest) || Objects.isNull(shipperId)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        Shipper shipper = shipperProfileMapper.selectById(shipperId);
        if (Objects.isNull(shipper)) {
            log.info("不存在的用户 "+shipperId);
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        if (Objects.isNull(putOnCargoRequest.getCargoShipperPhone()) || putOnCargoRequest.getCargoShipperPhone().length() == 0) {
            putOnCargoRequest.setCargoShipperPhone(shipper.getPhoneNumber());
        }
        if (Objects.isNull(putOnCargoRequest.getCargoReceiverPhone()) || putOnCargoRequest.getCargoReceiverPhone().length() == 0) {
            putOnCargoRequest.setCargoReceiverPhone(shipper.getPhoneNumber());
        }
        //调用系统接口上架
        return systemShipperCargoServiceRepo.cargoDeliver(putOnCargoRequest, shipperId);
    }

    /**
     * 成功支付货源金额，修改货源状态为上架
     *
     * @param shipperId 货主Id
     * @param cargoId   货源Id
     * @return 货源Id
     */
    @Override
    public Result<Long> payForCargoSuccess(Long shipperId, Long cargoId) throws Exception {
        if (Objects.isNull(shipperId) || Objects.isNull(cargoId)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        return systemShipperCargoServiceRepo.payForCargoSuccess(shipperId, cargoId);
    }

    /**
     * 下架
     *
     * @param cargoId 货源id
     * @return 货源Id
     */
    @Override
    public Result<Long> pullOffCargo(Long cargoId, Long shipperId) throws Exception {
        if (cargoId == null) {
            log.info("传入参数错误");
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        //调用系统接口下架
        Result<Long> cancelCargoResult = systemShipperCargoServiceRepo.cancelCargo(cargoId, shipperId);
        if (!cancelCargoResult.isSuccess()) {
            return Result.fail(null, cancelCargoResult.getCode(), cancelCargoResult.getMessage());
        }
        return Result.success(cargoId);
    }

    @Override
    public Result<ShipperCargoDetailResponse> getCargoDetailByCargoId(Long cargoId) {
        if (cargoId == null) {
            log.info("传入参数错误");
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        return systemShipperCargoServiceRepo.getCargoInfoByCargoId(cargoId);
    }

    @Override
    public Result<List<ShipperCargoBriefResponse>> getOnShelfCargosByShipperId(Long shipperId) {
        if (shipperId == null) {
            log.info("传入参数错误");
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        if (!shipperProfileMapper.existShipperId(shipperId)) {
            log.info("不存在的用户id");
            return Result.fail(null, ProfileCodeEnum.SHIPPER_NOT_EXIST.getCode(), ProfileCodeEnum.SHIPPER_NOT_EXIST.getMessage());
        }
        //调用系统接口
        Result<List<ShipperCargoBriefResponse>> onshelfCargosResult = systemShipperCargoServiceRepo.getOnshelfCargos(shipperId);
        if (!onshelfCargosResult.isSuccess() || onshelfCargosResult.getData() == null) {
            return Result.fail(null, onshelfCargosResult.getCode(), onshelfCargosResult.getMessage());
        }
        List<ShipperCargoBriefResponse> onshelfCargos = onshelfCargosResult.getData();
        onshelfCargos.sort((a, b) -> b.getCargoUpdateTime().compareTo(a.getCargoUpdateTime()));
        return Result.success(onshelfCargos);
    }

    @Override
    public Result<List<ShipperCargoBriefResponse>> getOffShelfCargosByShipperId(Long shipperId) {
        if (shipperId == null) {
            log.info("传入参数错误");
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        if (!shipperProfileMapper.existShipperId(shipperId)) {
            log.info("不存在的用户id");
            return Result.fail(null, ProfileCodeEnum.SHIPPER_NOT_EXIST.getCode(), ProfileCodeEnum.SHIPPER_NOT_EXIST.getMessage());
        }
        //调用系统接口
        Result<List<ShipperCargoBriefResponse>> offshelfCargosResult = systemShipperCargoServiceRepo.getOffshelfCargos(shipperId);
        if (offshelfCargosResult == null) {
            return Result.fail(null);
        }
        if (!offshelfCargosResult.isSuccess() || offshelfCargosResult.getData() == null) {
            return Result.fail(null, offshelfCargosResult.getCode(), offshelfCargosResult.getMessage());
        }
        List<ShipperCargoBriefResponse> offshelfCargos = offshelfCargosResult.getData();
        offshelfCargos.sort((a, b) -> b.getCargoUpdateTime().compareTo(a.getCargoUpdateTime()));
        return Result.success(offshelfCargos);
    }

    /**
     * 上传货源图片
     *
     * @param shipperId 货主Id
     * @param image     图片
     * @return 路径
     */
    @Override
    public Result<String> uploadCargoPhoto(Long shipperId, MultipartFile image) {
        if (shipperId == null || image == null) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR.getCode(), "入参错误");
        }
        return systemShipperCargoServiceRepo.uploadCargoPhoto(shipperId, image);
    }
}
